<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SqlString</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
		<style type="text/css">
			body{ overflow-x:hidden; overflow-y:hidden;}
		</style>
   </head>

<body>

<div >

    <div class=" p-3 mb-2 bg-primary text-white" >SqlString定制版</div>
    <dl class=" jumbotron row">

        <dt class="col-sm-6">
<!--            mb-2  -->
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">信息输入框</span>
                </div>
                <textarea id="sql" class="form-control" aria-label="With textarea" rows="10" cols="30" ng-change="name" ></textarea>
            </div>
        </dt>
        <dd class="col-sm-6">
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">转换后</span>
                </div>
                <textarea id="val" class="form-control" aria-label="With textarea" rows="10" cols="30" ng-change="name" ></textarea>
            </div>
        </dd>
        <dd class="col-sm-12">
            <table width="100%" border="0">
                <td width="50%">
                    <input  class="btn btn-success" type="button" value="美化SQL信息" id="changeBtn" onclick="getSqlStr()"/>
                    <input class="btn btn-info" type="button" value="拷贝SQL" onclick="copy()"/>
                    <input  class="btn btn-danger" type="reset" value="清空" id="cancleBtn" onclick="clearValue()" />
                </td>
                <td width="50%">
                    &nbsp;&nbsp;
                    <input class="btn btn-primary" type="button" value="转换" id="changeBtn" onclick="getStringBufferStr()"/>
                    <input class="btn btn-info" type="button" value="拷贝String" onclick="copyString()"/>

                </td>
            </table>
        </dd>
    </dl>
	<p class="text-center">作者： <a href="https://gitee.com/ruoyushen">ruoyushen</a></p>
	<p class="text-center">QQ： 2285421174</p>
	<p class="text-center">版本： 1.0.0-version</p>

</div>


<!--<script>-->
<!--    var app = angular.module('myApp', []);-->
<!--    app.controller('你想干嘛？', function($scope) {-->
<!--        $scope.demo = "John Doe";-->
<!--    });-->
<!--</script>-->
<script type="text/javascript">
    //    function document.onkeydown() {
    //        var e=event.srcElement;
    ////        alert(event.keyCode);
    //        if(event.keyCode==13) {

    //            document.getElementById("changeBtn").click();
    //            document.getElementById("cancleBtn").focus();
    //        } else if(event.keyCode==27) {
    //
    //            document.getElementById("cancleBtn").click();
    //            document.getElementById("changeBtn").focus();
    //        }
    //    }

    function getStringBufferStr(){

        var sql = document.getElementById("sql").value.toUpperCase();
        var str = "";
        if(sql.length != 0){
            str = editSQL(sql);
            document.getElementById("val").value = str;
            document.getElementById("val").createTextRange().execCommand('selectall');
        }

    }

    function getSqlStr(){

        var sb = document.getElementById("val").value;
        var str = "";
        if(sb.length != 0){
            var strss = sb.split("\n");
            for(var i = 0; i<strss.length; i++){
                var one = strss[i];
                if (one.indexOf("sql.append(") >= 0) {
                    one = one.replace(" \");","");
                    one = one.replace("sql.append(\" ","");
                    str += one + "\n";
                }
            }
            document.getElementById("sql").value = str;
            document.getElementById("sql").createTextRange().execCommand('selectall');
        }

    }

    String.prototype.trim   =   function() {
        //   用正则表达式将前后空格
        //   用空字符串替代。
        return   this.replace(/(^\s*)|(\s*$)/g,   "");
    }

    /**
     * 编辑输入的sql
     */
    function editSQL(sql) {

        var enterReg = /[\n]/g;
        var newRowReg = /[\r]/g;

        var stringReg = new RegExp("['][^']*[']");
        var numberReg = new RegExp("[0-9]+");
        var changedsql = "";
        var arrs = "";

        sql = sql.replace(newRowReg, "");
        sql = sql.replace(enterReg,"#");
        sql = trin(sql);

        var arraySQL = sql.split("#");
        for (i = 0; i< arraySQL.length; i++) {

            arrs = trin(arraySQL[i]).split(" ");

            var sql = trin(arraySQL[i]);

            if (sql.trim() != "") {
                // 拼接成java中执行的sql
                changedsql = changedsql + "sql.append(\" " + sql + " \");" + "\n";
            }
        }
        if (changedsql != "") {
            var javaNewBuffer = "StringBuffer sql = new StringBuffer();\r";

            var args = "";
            if (changedsql.indexOf("?") > 0) {
                args = "Object[] args = new Object[]{ ?,?,? };\n";

                // 如果是查询语句，生成查询语句
                // if (changedsql.indexOf("select") > 0) {
                //     javaExSql = "List queryList = getJdbcTemplate().queryForList(sql.toString(), args);\n";
                // } else {
                //     javaExSql = "getJdbcTemplate().update(sql.toString(), args);\n"
                // }
            } else {
                // 如果是查询语句，生成查询语句
                // if (changedsql.indexOf("select") > 0) {
                //     javaExSql = "List queryList = getJdbcTemplate().queryForList(sql.toString());\n";
                // } else {
                //     javaExSql = "getJdbcTemplate().update(sql.toString());\n"
                // }
            }
        }

        return javaNewBuffer + changedsql + "\n" + args ;
    }

    /**
     * 去掉空行
     */
    function trin(str){
        var re = /\n/g;
        str = str.replace(re, "");
        re = /\r/g;
        str = str.replace(re, "");
        return str;
    }

    function copy(){
        var text = document.getElementById("sql").value;
        window.clipboardData.setData("Text", text);

    }
    function copyString(){
        var text = document.getElementById("val").value;
        console.log(text)
        window.clipboardData.setData("Text", text);
     }


    function clearValue() {

        var obj = document.getElementById("sql");

        obj.value="";
        obj.focus();
        document.getElementById("val").value = "";
    }


</script>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>



</body>
</html>
